#! /usr/bin/env python
#coding=utf-8
# t_entity
import t_player
import t_mapmgr
import base_object

inst = None
def init():
	global inst
	if not inst:
		inst = CEntityMgr()

def destroy():
	global inst
	if inst:
		inst.destroy()
		inst = None

#游戏实体管理
class CEntityMgr(base_object.CObject):
	def __init__(self):
		self.g_entitys = {}
		self.g_phys = {}
		pass

	def addentity(self, playerinfo, AItype = None):
		import t_player
		entity = t_player.CPlayer(playerinfo)
		self.g_entitys[playerinfo.uid] = entity
		self.g_phys[entity.obj.phy.id] = entity
		return self.g_entitys[playerinfo.uid]

	def delentity(self, uid = None,id = None):
		if uid:
			# 玩家死亡
			if uid == t_mapmgr.inst.objMe.player.uid:
				t_mapmgr.inst.playerdead()
				return
			id = self.g_entitys[uid].obj.phy.id
			self.g_entitys[uid].destroy()
			self.g_entitys[uid] = None
			self.g_phys[id] = None
			del self.g_entitys[uid]
			del self.g_phys[id]

		elif id:
			uid = self.g_phys[id].player.uid
			self.g_phys[id].destroy()
			self.g_entitys[uid] = None
			self.g_phys[id] = None
			del self.g_entitys[uid]
			del self.g_phys[id]

	def destroy(self):
		import copy
		copyg_e = copy.copy(self.g_entitys)
		for key in copyg_e:
			id = self.g_entitys[key].obj.phy.id
			self.g_entitys[key].destroy()
			del self.g_entitys[key]
			del self.g_phys[id]
		pass

	def update(self):
		for entity in self.g_entitys:
			self.g_entitys[entity].update()